package com.mi.fastbird.mybstis.config;

import com.mi.fastbird.common.util.HumpKit;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.wrapper.MapWrapper;
import org.apache.ibatis.reflection.wrapper.ObjectWrapper;
import org.apache.ibatis.reflection.wrapper.ObjectWrapperFactory;

import java.util.Map;

/**
 * 驼峰转换处理器
 *
 * @author 王永吉
 * @date 2019/10/31
 */
public class HumpHandler implements ObjectWrapperFactory {
    @Override
    public boolean hasWrapperFor(Object o) {
        return o instanceof Map;
    }

    @Override
    public ObjectWrapper getWrapperFor(MetaObject metaObject, Object o) {
        return new MiMapWrapper(metaObject, (Map) o);
    }

    static class MiMapWrapper extends MapWrapper {
        MiMapWrapper(MetaObject metaObject, Map<String, Object> map) {
            super(metaObject, map);
        }

        @Override
        public String findProperty(String name, boolean useCamelCaseMapping) {
            if (useCamelCaseMapping) return HumpKit._ToHump(name);
            return name;
        }
    }
}